package com.huffman.smart_picture.controller;

import com.huffman.smart_picture.common.auth.SessionContext;
import com.huffman.smart_picture.common.auth.annotation.CheckRole;
import com.huffman.smart_picture.common.UserSession;
import com.huffman.smart_picture.common.base.reponse.ResponseResult;
import com.huffman.smart_picture.domain.constants.CommonConstant;
import com.huffman.smart_picture.manager.CosManager;
import com.huffman.smart_picture.manager.FileManager;
import com.huffman.smart_picture.manager.PictureUploadDto;
import com.huffman.smart_picture.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * @author DengLin
 * @title 测试
 * @date 2024/06/27
 */
@RestController
@Slf4j
public class TestController {

    @Resource
    private SessionContext sessionContext;

    @Resource
    private CosManager cosManager;

    @Resource
    private FileManager fileManager;

    @PostMapping("/test")
//    @CheckAuth(roles = {CommonConstant.ADMIN})
    public ResponseResult test(String string) {
        return ResponseUtil.success();
    }

    @PostMapping("/test/picture")
    @CheckRole(roles = {CommonConstant.ADMIN})
    public ResponseResult uploadPicture(@RequestPart("file") MultipartFile multipartFile){
        UserSession userSession = sessionContext.getUserSession();
        String userId = userSession.getId().toString();
        String uploadPrefix = String.format("public/%s", userId);
        PictureUploadDto pictureUploadDto = fileManager.uploadPicture(multipartFile, uploadPrefix);
        return ResponseUtil.success(pictureUploadDto.getUrl());
    }
}
